home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Graphics Plus
/
Graphics Plus.iso
/
general
/
viewers
/
polyview
/
hdf-ucd.lha
/
HDF-UCD
/
util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-03
|
12KB
|
369 lines
#include "basetype.h"
#include "util.h"
/*can be defined as a macro later */
/*******************************************************************************
* function: int EVEN(v)
* description: returns true if v is even, false otherwise
* parameters: v
* return value: 1 or 0 depending on v even or odd
* modified parameters: none
*******************************************************************************/
int EVEN(v)
{
if ((v % 2) == 0) return(1); return(0);
}
/*******************************************************************************
* procedure: void handleerror(errnum)
* description: prints a message associated with errnum.
* parameters: errnum
* return value: none
* modified parameters: none
*******************************************************************************/
void handleerror(errnum)
int errnum;
{
switch (errnum) {
case 100: printf("illegal face type \n"); break;
case 200: printf("memory allocation failed \n"); break;
case 300: printf("Finite Element type not yet supported \n"); break;
case 400: printf("Connectivity data file format not OK \n"); break;
case 500: printf("Error opening data file \n"); break;
case 600: printf("\nFailed to Match Faces(version #)\n");
printf("Check Sequence Set Calls\n"); break;
}
exit(0);
}
/*******************************************************************************
* procedure: char *MemAlloc(size)
* description: allocates size bytes and returns a pointer to memory
* parameters: size
* return value: Str (pointer to memory)
* modified parameters: none
*******************************************************************************/
char *MemAlloc(size)
int size;
{
char *Str;
if ( (Str = (char *) malloc(size)) == NULL) handleerror(200);
return(Str);
}
/*******************************************************************************
* procedure: void MemRelease(Str)
* description: releases memory pointed to by Str
* parameters: Str
* return value: none
* modified parameters: none
*******************************************************************************/
void MemRelease(Str)
char *Str;
{
free(Str);
}
/*******************************************************************************
* procedure: void inithashtabs()
* description: initializes the hash tables used for
* processing faces of cells in mesh.
* parameters: none
* return value: none
* modified parameters: quadhashtab, trihashtab
*******************************************************************************/
void inithashtabs(maxvertices)
int maxvertices;
{
int i;
trihashtab = (triface **)MemAlloc(sizeof(triface *)*maxvertices);
quadhashtab =(quadface **) MemAlloc(sizeof(quadface *)*maxvertices);
for (i = 0; i < maxvertices; i++)
{quadhashtab[i] = NULL; trihashtab[i] = NULL;}
}
/*******************************************************************************
* procedure: void freehashtabs()
* description: frees memory allocated for hash tables.
* parameters: none
* return value: none
* modified parameters: quadhashtab, trihashtab
*******************************************************************************/
void freehashtabs(maxvertices)
int maxvertices;
{
int i;
quadface *qf, *tqf; triface *tf, *ttf;
for (i = 0; i < maxvertices; i++) {
qf = tqf = quadhashtab[i];
if (qf != NULL) {
while (qf->Next != NULL) {
tqf = qf; qf = qf->Next;
MemRelease(tqf);
} MemRelease (qf);
}
tf = ttf = trihashtab[i];
if (tf != NULL) {
while (tf->Next != NULL) {
ttf = tf; tf = tf->Next;
MemRelease(ttf);
} MemRelease (tf);
}
}
MemRelease(quadhashtab); MemRelease(trihashtab);
}
/*******************************************************************************
* function: int scrambleij(i, j)
* description: packs i and j into one word and returns that value.
* parameters: i, j
* _____
* return value: |i|j|
* -----
* modified parameters: none
*******************************************************************************/
int scrambleij (i, j)
int i, j;
{
int n = 0;
n = n | abs(i); n = (n << 16) | abs(j); return(n);
/* 16 == half of word size = 32
16 bits for i, 16 bits for j, packed into one word */
}
/*******************************************************************************
* procedure: void unscrambleij(n, i, j)
* description: splits n into i and j.
* parameters: n, i, j
* return value: none
* modified parameters: i, j
*******************************************************************************/
void unscrambleij (n, i, j)
int n, *i, *j;
{
n = abs(n);
*j = n & umask1; *i = (n >> 16);
/* 16 == half of word size = 32
j = lower 16 bits, i = higher 16 bits */
}
/*******************************************************************************
* function: int scramblefv(f, v)
* description: packs f and v into one word and returns that value.
* parameters: f, v
* _____________
* return value: |f |v|
* -------------
* modified parameters: none
*******************************************************************************/
int scramblefv (f, v)
int f, v;
{
int n = 0;
n = n | abs(f); n = (n << 3) | abs(v); return(n);
/* 29 bits for f, 3 bits for v */
}
/*******************************************************************************
* procedure: void unscramblefv(n, f, v)
* description: splits n into f and v.
* parameters: n, f, v
* return value: none
* modified parameters: f, v
*******************************************************************************/
void unscramblefv (n, f, v)
int n, *f, *v;
{
n = abs(n);
*v = n & umask2; *f = (n >> 3);
/* 29 bits for f, 3 bits for v */
}
/*******************************************************************************
* function: int findmin(fv, num)
* description: finds the minimum of the numbers in array fv.
* size of fv given by num.
* parameters: fv, num
* return value: minimum value
* modified parameters: none
*******************************************************************************/
int findmin(fv, num)
int *fv, num;
/* num is either 4 quadfaces, or 3 trifaces */
{
int min, i;
min = fv[0];
for (i = 1; i < num; i++) if (fv[i] < min) min = fv[i];
return (min);
}
/*******************************************************************************
* procedure: void qstoreface(facevertices, index)
* description: stores face given by facevertices, index in hash table.
* parameters: facevertices, index
* return value: none
* modified parameters: quadhashtab(in util.h)
*******************************************************************************/
void qstoreface(facevertices, index)
int *facevertices, index;
{
quadface *f; int min;
min = findmin(facevertices, 4); /* 4= # vertices of quad face */
f = (quadface *) MemAlloc(SizeOfquadface);
f->i = facevertices[0]; f->j = facevertices[1];
f->k = facevertices[2]; f->l = facevertices[3];
f->faceindex = index; f->Next = quadhashtab[min];
quadhashtab[min] = f;
}
/*******************************************************************************
* procedure: void tstoreface(facevertices, index)
* description: stores face given by facevertices in hash table.
* parameters: facevertices, index
* return value: none
* modified parameters: trihashtab(in util.h)
*******************************************************************************/
void tstoreface(facevertices, index)
int *facevertices, index;
{
triface *f; int min;
min = findmin(facevertices, 3); /* 3=#of vetices of tri face */
f = (triface *) MemAlloc(SizeOftriface);
f->i = facevertices[0]; f->j = facevertices[1];
f->k = facevertices[2];
f->faceindex = index; f->Next = trihashtab[min];
trihashtab[min] = f;
}
/*******************************************************************************
* function: BOOLEAN qinlist (v, f)
* description: checks if vertex v is one of the vertices of face f.
* parameters: v, f
* return value: True or False
* modified parameters: none
*******************************************************************************/
BOOLEAN qinlist (v, f)
int v;
quadface *f;
{
if ((v == f->i) || (v == f->j) || (v == f->k) || (v == f->l)) return(True);
return(False);
}
/*******************************************************************************
* function: BOOLEAN tinlist (v, f)
* description: checks if vertex v is one of the vertices of face f.
* parameters: v, f
* return value: True or False
* modified parameters: none
*******************************************************************************/
BOOLEAN tinlist (v, f)
int v;
triface *f;
{
if ((v == f->i) || (v == f->j) || (v == f->k)) return(True);
return(False);
}
/*******************************************************************************
* function: BOOLEAN qequal(facevertices, findex, f)
* description: checks if facevertices and f refer to the same face.
* parameters: facevertices, findex, f
* return value: True or False
* modified parameters: findex
*******************************************************************************/
BOOLEAN qequal(facevertices, findex, f)
int *facevertices, *findex;
quadface *f;
{
int i;
for (i = 0; i < 4; i++) {
if (!(qinlist(facevertices[i], f))) return(False);
}
*findex = f->faceindex;
return (True); /* all the 4 vertices are there */
}
/*******************************************************************************
* function: BOOLEAN tequal(facevertices, findex, f)
* description: checks if facevertices and f refer to the same face.
* parameters: facevertices, findex, f
* return value: True or False
* modified parameters: findex
*******************************************************************************/
BOOLEAN tequal(facevertices, findex, f)
int *facevertices, *findex;
triface *f;
{
int i;
for (i = 0; i < 3; i++) {
if (!(tinlist(facevertices[i], f))) return(False);
}
*findex = f->faceindex;
return (True); /* all the 4 vertices are there */
}
/*******************************************************************************
* function: BOOLEAN qsearchface(facevertices, findex)
* description: searches for the face given by face
* vertices in the hashtable.
* parameters: facevertices, findex
* return value: True or False
* modified parameters: findex
*******************************************************************************/
BOOLEAN qsearchface(facevertices, findex)
int *facevertices, *findex;
{
int min; quadface *f;
min = findmin(facevertices, 4);
f = quadhashtab[min];
while (f != NULL) {
if (qequal (facevertices, findex, f)) return (True); /* face is there */
f = f->Next;
}
return(False); /* face is not there */
}
/*******************************************************************************
* function: BOOLEAN tsearchface(facevertices, findex)
* description: searches for the face given by face
vertices in the hashtable.
* parameters: facevertices, findex
* return value: True or False
* modified parameters: findex
*******************************************************************************/
BOOLEAN tsearchface(facevertices, findex)
int *facevertices, *findex;
{
int min; triface *f;
min = findmin(facevertices, 3);
f = trihashtab[min];
while (f != NULL) {
if (tequal (facevertices, findex, f)) return (True); /* face is there */
f = f->Next;
}
return(False); /* face is not there */
}